/*
 * Copyright 2010 Pieter De Rycke
 *
 * This file is part of Frozen Bubble XNA.
 *
 * Frozen Bubble XNA is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2, as published by the Free Software Foundation.
 *
 * Frozen Bubble XNA is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with Frozen Bubble XNA. If not, see 
 * <http://www.gnu.org/licenses/>.
 *
 * Artwork:
 *    Alexis Younes <73lab at free.fr>
 *      (everything but the bubbles)
 *    Amaury Amblard-Ladurantie <amaury at linuxfr.org>
 *      (the bubbles)
 *
 * Soundtrack:
 *    Matthias Le Bidan <matthias.le_bidan at caramail.com>
 *      (the three musics and all the sound effects)
 *
 * Design & Programming:
 *    Guillaume Cottenceau <guillaume.cottenceau at free.fr>
 *      (design and manage the project, whole Perl sourcecode)
 *
 * XNA and Windows Phone 7 port:
 *    Pieter De Rycke
 *
 * Websites:
 *    http://www.frozen-bubble.org/
 *    http://pieterderycke.wordpress.com/
 */

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace FrozenBubble
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class Raster : GameScreen
    {
        private IPlayerDimensions _dimensions;
        private IEnumerable<Rectangle> _lines;
        private Color _color;
        private int _borderWidth;

        private Texture2D _pixel;

        public Raster(IPlayerDimensions dimensions, int width)
        {
            _dimensions = dimensions;
            _color = Color.LawnGreen;
            _borderWidth = width;

            Initialize();
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        private void Initialize()
        {
            int offset = _borderWidth / 2;

            List<Rectangle> lineList = new List<Rectangle>();

            int width = _dimensions.RightLimit - _dimensions.LeftLimit;
            int arrayHeight = 12;   // 11 rows of bubbles ( +  1 for top border)

            for (int row = 0; row <= arrayHeight; row++)
            {
                int y = (int)(_dimensions.InitialTopLimit + row * BubbleConstants.ROW_SIZE);

                lineList.Add(new Rectangle(_dimensions.LeftLimit, y - offset, width, _borderWidth));

                if (row < arrayHeight)
                {
                    for (int col = 0; col < 10; col++)
                    {
                        int x = (int)(_dimensions.LeftLimit + col * BubbleConstants.BUBBLE_SIZE +
                            BubbleConstants.Odd(row) * BubbleConstants.BUBBLE_SIZE / 2);

                        if (x <= _dimensions.RightLimit)
                            lineList.Add(new Rectangle(x - offset, y, _borderWidth, (int)BubbleConstants.ROW_SIZE));
                    }
                }
            }

            _lines = lineList;
        }

        public override void LoadContent()
        {
#if WINDOWS_PHONE
            _pixel = new Texture2D(Game.GraphicsDevice, 1, 1);
#else
            _pixel = new Texture2D(Game.GraphicsDevice, 1, 1, 1, TextureUsage.None, SurfaceFormat.Color);
#endif
            _pixel.SetData<Color>(new Color[] { _color });
            
            base.LoadContent();
        }

        public override void Draw(GameTime gameTime)
        {
            //_spriteBatch.Begin();

            foreach (Rectangle line in _lines)
                SpriteBatch.Draw(_pixel, line, Color.White);

            //_spriteBatch.End();

            //base.Draw(gameTime);
        }
    }
}